﻿Public Class TimeSheetExplorer
    Inherits ExplorerTreeList


    Private aPL As PLBase

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
    End Sub

    Public Sub New()
        MyBase.New()
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
    End Sub

    Public Overrides Sub PopulateDetailSection()
        'Public Overrides Sub PopulateDetailSection(ByVal aguid As Guid, Optional ByVal mt As MetaDataTable = Nothing)
        Me.Cursor = Cursors.WaitCursor
        '
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If
        '

        Dim frmCaption As String = String.Empty
        Dim guidEnfant As Guid = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
        Dim aMQ = MetaQuery.GetMetaQueryByName(NodeDataRow("MetaQuerie").ToString)

        aPL = PLBase.CreateInstanceOf(NodeDataRow("PL").ToString(), aMQ.QueryName)

        If Not IsDBNull(NodeDataRow("Employee")) Then
            If NodeDataRow.Table.Columns.Contains("Employee") Then
                frmCaption = NodeDataRow("Employee").ToString
            Else
                frmCaption = "Employee"
            End If

        Else
            If NodeDataRow.Table.Columns.Contains("Code") Then
                frmCaption = NodeDataRow("Code").ToString
            Else
                frmCaption = "Code"
            End If

        End If

        If FocusedNodeLevel = 0 AndAlso Not guidEnfant.Equals(Guid.Empty) Then
            aPL.SQLWhere = String.Format("{0} = '{1}'", "TimeSheetStatusID_FK", guidEnfant)
        ElseIf FocusedNodeLevel = 1 AndAlso Not guidEnfant.Equals(Guid.Empty) Then
            aPL.SQLWhere = String.Format("{0} = '{1}'", aMQ.GUIDField, guidEnfant)
        End If

        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aPL, .SecureGrid = True, .Name = aMQ.Caption, .FormCtrlCD = MyBase.PanelName}


        '**********************************
        If FocusedNodeLevel = 1 Then
            '-------- Colle PL-----------------'
            AddHandler aGridControl.ValidateMasterRow, AddressOf ValidateMasterRow
            AddHandler aGridControl.ValidateDetailRow, AddressOf ValidateDetailRow
            AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewMasterRow
            AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewDetailRow
            AddHandler aGridControl.MasterRowNotify, AddressOf RowNotify

            AddHandler aGridControl.DeleteRow, AddressOf DeleteRow
            '
            AddHandler aGridControl.ProcessCurrentMasterRecord, AddressOf ProcessCurrentMasterRecord
            AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf ProcessCurrentDetailRecord
            AddHandler aGridControl.ShownMasterEditor, AddressOf ShownMasterEditor
            AddHandler aGridControl.HiddenMasterEditor, AddressOf HiddenMasterEditor
            '
            AddHandler aGridControl.ShownDetailEditor, AddressOf ShownDetailEditor
            AddHandler aGridControl.HiddenDetailEditor, AddressOf HiddenDetailEditor

            '----- Actions ---------------'

            Dim action As ActionOption

            'submit
            action = New ActionOption("Timesheet", "ACT_00140", "Submit", AddressOf MenuSubmit, True, True)
            aGridControl.GridActionOptionList.Add(action)

            'Unsbmit'
            action = New ActionOption("Timesheet", "ACT_00141", "UnSubmit", AddressOf MenuUnSubmit, True, True)
            aGridControl.GridActionOptionList.Add(action)

            'Reject'
            action = New ActionOption("Timesheet", "ACT_00142", "Reject", AddressOf MenuReject, True, True)
            aGridControl.GridActionOptionList.Add(action)

            'Reviewed'
            action = New ActionOption("Timesheet", "ACT_00143", "Reviewed", AddressOf MenuReviewed, True, True)
            aGridControl.GridActionOptionList.Add(action)

            'Reviewed'
            action = New ActionOption("Timesheet", "ACT_00144", "Approve", AddressOf MenuApproved, True, True)
            aGridControl.GridActionOptionList.Add(action)

            '--------End Action------------'


            'More to come......
            '--------Colle End-----------'
            'Cette ligne ne faits pas de sens car l'id du user a rien a voir avec l'emplId,... BD 2012-09-15
            'TimeSheetCase.EmplID = CommonExt.Base.AppManager.UserID

            aGridControl.ExpandFirstMasterRow = True
            ClientMode = False
        End If


        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next
        '
        Me.Cursor = Cursors.Default
    End Sub


    ''' <summary>
    ''' CopyTimesheetEntry
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to copy an timesheetline to a new COPY of this timesheetentry date called COPY_ ...</remarks>
    Public Overridable Sub CopyTimesheetEntry(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag.CurrentSelectedDataRow, DataRow)
        Dim newName As String = String.Format("Copy of '{0}'", aDataRow("DateWorked").ToString)



        Try
            TimeSheetHour_PL.CopyActivity(CType(aDataRow("TimeCardDetailID"), Guid), newName)
            'MessageWindow.ShowMessage(String.Format("Copy of '{0}'  Done", aDataRow("DateWorked")), MsgBoxStyle.Information)

            Dim aview As ColumnView = aGridControl.GridControl1.FocusedView
            Dim aMt As MetaDataTable = DirectCast(DirectCast(aview.DataSource, DataView).Table, MetaDataTable)
            aMt.FillBusinessApplicationData()
            aview.RefreshData()

        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try

    End Sub


    
    Private Sub TimeSheetExplorer_Load(sender As Object, e As EventArgs) Handles Me.Load
        TreeActionOptionListLoad()
        MetaControlTreeListActionOptionList.ApplySecurity()
    End Sub
End Class
